package com.gitee.kenewstar.migration.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.kenewstar.migration.constants.Const;
import com.gitee.kenewstar.migration.entity.Profile;
import com.gitee.kenewstar.migration.entity.ProfileTl;
import com.gitee.kenewstar.migration.entity.ProfileValue;
import com.gitee.kenewstar.migration.mapper.ProfileMapper;
import com.gitee.kenewstar.migration.mapper.ProfileTlMapper;
import com.gitee.kenewstar.migration.mapper.ProfileValueMapper;
import com.gitee.kenewstar.migration.service.ProfileService;
import com.gitee.kenewstar.migration.util.Util;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author kenewstar
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProfileServiceImpl implements ProfileService {

    private final ProfileMapper profileMapper;
    private final ProfileTlMapper profileTlMapper;
    private final ProfileValueMapper profileValueMapper;


    @Override
    @DS(Const.DEV)
    public List<Profile> devSelectProfileList(List<Profile> list) {
        QueryWrapper<Profile> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("profile_name", list.stream().map(Profile::getProfileName).collect(Collectors.toSet()));
        queryWrapper.in(Const.TENANT_ID, list.stream().map(Profile::getTenantId).collect(Collectors.toSet()));
        List<Profile> profileList = this.profileMapper.selectList(queryWrapper);

        Map<String, Long> data = list.stream().collect(Collectors.toMap(x -> x.getProfileName() + "-" + x.getTenantId(), Profile::getTenantId));
        profileList = profileList.stream().filter(x -> Objects.nonNull(data.get(x.getProfileName() + "-" + x.getTenantId()))).collect(Collectors.toList());

        return profileList;
    }

    @Override
    @DS(Const.DEV)
    public List<ProfileTl> devSelectProfileTl(List<Long> profileIds) {
        QueryWrapper<ProfileTl> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("profile_id", profileIds);
        return this.profileTlMapper.selectList(queryWrapper);
    }

    @Override
    @DS(Const.DEV)
    public List<ProfileValue> devSelectProfileValue(List<Long> profileIds) {
        QueryWrapper<ProfileValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("profile_id", profileIds);
        return this.profileValueMapper.selectList(queryWrapper);
    }

    @Override
    @DS(Const.UAT)
    @Transactional(rollbackFor = Exception.class)
    public void uatInsertProfile(List<Profile> list) {
        list.forEach(x -> x.setTenantId(Util.getTenant(x.getTenantId())));
        list.forEach(this.profileMapper::insert);
    }

    @Override
    @DS(Const.UAT)
    @Transactional(rollbackFor = Exception.class)
    public void uatInsertProfileTl(List<ProfileTl> list) {
        list.forEach(x -> x.setTenantId(Util.getTenant(x.getTenantId())));
        list.forEach(this.profileTlMapper::insert);
    }

    @Override
    @DS(Const.UAT)
    @Transactional(rollbackFor = Exception.class)
    public void uatInsertProfileValue(List<ProfileValue> list) {
        list.forEach(x -> x.setTenantId(Util.getTenant(x.getTenantId())));
        list.forEach(this.profileValueMapper::insert);
    }
}
